<link rel="stylesheet" href="/distribute/template/static/css/search.css">
<link rel="stylesheet" href="/packages/resource/pintuer/pintuer.css">
<div class="box pintuer">
    <div class="search" style="display: none;">
        <a href="distribute_center.php?action=goods_list&type=cat" class="cat">分类查询</a>
        <a href="distribute_center.php?action=goods_list&type=store" class="store">店铺查询</a>
    </div>

    <form action="">
        <input type="hidden" name="action" value="goods_list">
        <input type="hidden" name="type" value="<?php echo $_GET['type'] ?>">
        <?php
            if ($_GET['type'] == 'cat') {

                ?>
                <div class="form-group">
                    <div class="label">
                        分类查询：
                    </div>
                    <div class="field">
                        <?php
                            $list = Model('goods_class')->getTreeClassList();
                        ?>
                        <select class="input " style="margin-left: 0;" name="cat1" id="">
                            <option value="-1">请选择一级分类</option>
                            <?php
                                foreach ($list as $li) {
                                    if ($li['deep'] !== 1) {
                                        continue;
                                    }
                                    ?>
                                    <option value="<?php echo $li['gc_id'] ?>"
                                            parent_id="<?php echo $li['gc_parent_id'] ?>"><?php echo $li['gc_name'] ?></option>
                                    <?php
                                }
                            ?>
                        </select>
                        <select class="input margin-top" name="cat2" id="">
                            <option value="-1">请选择二级分类</option>
                            <?php
                                foreach ($list as $li) {
                                    if ($li['deep'] !== 2) {
                                        continue;
                                    }
                                    ?>
                                    <option value="<?php echo $li['gc_id'] ?>"
                                            parent_id="<?php echo $li['gc_parent_id'] ?>"><?php echo $li['gc_name'] ?></option>
                                    <?php
                                }
                            ?>
                        </select>
                        <select class="input margin-top" name="cat3" id="">
                            <option value="-1">请选择三级分类</option>
                            <?php
                                foreach ($list as $li) {
                                    if ($li['deep'] !== 3) {
                                        continue;
                                    }
                                    ?>
                                    <option value="<?php echo $li['gc_id'] ?>"
                                            parent_id="<?php echo $li['gc_parent_id'] ?>"><?php echo $li['gc_name'] ?></option>
                                    <?php
                                }
                            ?>
                        </select>
                    </div>
                </div>
                <script>
                    $('.field select').change(function () {
                        var val = $(this).val();
                        var next = $(this).next();
                        if (!next.length) {
                            return;
                        }
                        next.find('option:gt(0)').hide();
                        next.find("option[parent_id=" + val + "]").show();
                        var index = $(this).index();
//                        $('.line select:gt(' + (index) + ')').each(function () {
//                            $(this).find('option:gt(0)').hide()
//                            $(this).find('option:eq(0)').attr('selected', 'selected').siblings().removeAttr('selected');
//                        });
                        next.find('option:eq(0)').attr('selected', 'selected').siblings().removeAttr('selected');

                    });

                    $('[name=cat1] option').show();
                </script>
                <div class="form-group margint-top">
                    <div class="label">
                        店铺名称：
                    </div>

                    <div class="field">
                        <input type="text" class="input" name="store_kw" placeholder="请输入店铺关键字"
                               value="<?php echo $_GET['store_kw']; ?>">
                    </div>

                </div>
                <div class="form-group">
                    <div class="label">
                        商品名称：
                    </div>
                    <div class="field">
                        <input type="text" class="input" name="kw" placeholder="请输入商品关键字" <?php echo $_GET['kw'] ?>>
                    </div>


                </div>
                <div class="margin-top">
                    <button class="button bg-blue " style="width: 100%;" type="submit" >
                        检索
                    </button>
                </div>

            <?php
                }
                else{
            ?>
                <div class="line">
                    <label for="">
                        商品名称：
                    </label>
                    <input type="text" name="store_kw" placeholder="请输入店铺关键字">
                    <input type="submit">
                </div>
                <?php
            }
        ?>

    </form>
</div>
<div class="box">

    <div class="goods_list">
        <?php
            $m = Model('goods_common');
            $condition = array(
                'agree_distribute' => 1,
                'goods_state'      => 1,
                'goods_verify'     => 1,

            );
            if ($_GET['cat3'] > -1) {
                $condition['gc_id_3'] = $_GET['cat3'];
            }
            if ($_GET['cat2'] > -1) {
                $condition['gc_id_2'] = $_GET['cat2'];
            }
            if ($_GET['cat1'] > -1) {
                $condition['gc_id_1'] = $_GET['cat1'];
            }
            if (!empty($_GET['kw'])) {
                $condition['goods_name'] = array(
                    'like',
                    '%' . trim($_GET['kw']) . '%'
                );
            }

            if (!empty($_GET['store_kw'])) {
                $stores = Model('store')->where(array(
                    'store_name' => array(
                        'like',
                        "&{$_GET['store_kw']}&"
                    )
                ))->select();
                if ($stores) {
                    $condition['store_id'] = array(
                        'in',
                        implode(array_map(function ($item) {
                            return $item['store_id'];
                        }, $stores))
                    );
                }
            }


            $list = $m->where($condition)->page(10)->order("goods_addtime desc")->select();
            $distribute = new Distribute($minfo['member_id']);
            if ($list) {

                foreach ($list as $item) {

                    $ginfo = Model('goods')->where(array('goods_commonid' => $item['goods_commonid']))->find();
                    $gminfo = Model()->table('goods_common')->where([
                        'goods_commonid' => $item['goods_commonid']
                    ])->find();

                    ?>
                    <div class="item" goods_id="<?php echo $ginfo['goods_id'] ?>">
                        <div class="img">
                            <img
                                src="/data/upload/shop/store/goods/<?php echo $item['store_id'] ?>/<?php echo $item['goods_image'] ?>"
                                alt="">
                        </div>

                        <div class="info">
                            <p class="name">
                                <?php echo $item['goods_name'] ?>
                                <?php
                                    if ($distribute->checkGoods($ginfo['goods_id'])) {
                                        echo '（已添加）<i class="added"></i>';
                                    }
                                ?>

                            </p>

                            <p class="price">
                                销售价格：￥<?php

                                    if($gminfo['goods_distribute'] > 0){
                                        echo $gminfo['goods_distribute'];
                                    }
                                    else{
                                        echo $item['goods_price'];
                                    }
                                ?>
                            </p>

                            <p class="price">
                                分销利润：￥<?php echo $distribute->calMarginByGoods($ginfo['goods_id']) ?>
                            </p>
                        </div>

                        <i class="selected fa fa-check"></i>
                    </div>
                    <?php
                }
                ?>
                <link rel="stylesheet" href="/distribute/template/static/css/pager.css"/>

                <div>
                    <a href="javascript:;" class="confirm_add">确定添加</a>
                    <link rel="stylesheet" href="/packages/resource/css/pager.css">
                    <div class="_pager">
                        <?php echo $m->showpage() ?>
                    </div>
                </div>

                <?php

            } else {
                ?>
                <div class="tips">
                    没有找到符合条件的商品
                </div>
                <?php
            }
        ?>
    </div>
</div>

<script>
    $('.item').click(function () {
        if ($(this).find('i.added').length) {
            layer.msg("该商品已经添加！");
            return;
        }
        $(this).find('.selected').toggle();
    });

    var sel = {
        cat1: '<?php echo $_GET['cat1'];?>',
        cat2: '<?php echo $_GET['cat2'];?>',
        cat3: '<?php echo $_GET['cat3'];?>'
    }

    for (var i in sel) {
        var item = $('[name=' + i + ']');
        var target = item.find('option[value=' + sel[i] + ']');
        if (target.length) {
            target.attr('selected', 'selected').siblings().removeAttr('selected');
            item.trigger('change');
        }
    }


    $('.confirm_add').click(function () {
        var gids = [];
        $('.goods_list .item').each(function () {
            if ($(this).find(".selected").css('display') != 'none') {
                gids.push($(this).attr('goods_id'));
            }
        });

        if (!gids.length) {
            layer.msg("您还没有选择任何商品");
            return;
        }
        var index = layer.load();

        $.post("/shop/index.php?act=seccode&op=proxy_shop_add_more_goods", {gids: gids}, function (msg) {
            layer.close(index);
            if (msg == 'success') {
                layer.msg("添加成功");
            }
            else {
                layer.msg("添加失败");
            }
        })
    })
</script>

